<template lang="pug">
.designer
  .header
    .header-search
      el-input(placeholder="用户账户/手机号" prefix-icon="el-icon-search" clearable v-model="searchString" @change="changeSearch")
      el-select(placeholder="选择用户状态" prefix-icon="el-icon-search" clearable v-model="roleId" @change="changeStatus")
        el-option(v-for="item in roleList", :key="item.roleId", :label="item.roleName", :value="item.roleId")
    .header-btn
      el-button(type="primary" icon="el-icon-plus" size="small" @click="addMember" v-if="$permission('AB-06001-001')") 添加
  .designer-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      empty-text="未找到符合条件的内容"
    )
      el-table-column(type="index" width="55" align="center" label="序号")
      el-table-column(label="姓名" prop="employeeName")
        template(slot-scope="scope")
          .table-item-bd.flex.click(@click="seeDetail(scope.row)")
            .table-item-name {{scope.row.employeeName}}
      el-table-column(label="账号" prop="userName")
      el-table-column(label="手机号" prop="mobile")
      el-table-column(label="邮箱" prop="email")
      el-table-column(label="成员状态" prop="employeeStatus")
        template(slot-scope="scope")
          .table-item-status(v-if="!scope.row.employeeStatus")
            .iconfont.icon-normal
            span 启用
          .table-item-status(v-else)
            .iconfont.icon-block-up
            span 停用
      el-table-column(label="操作" prop="")
        template(slot-scope="scope")
          .table-item-bd
            .table-item-handle
              .table-item-btn(:class="scope.row.employeeStatus ? 'normal' : 'warning'" @click="setStatus(scope.row)" v-if="$permission('AB-06001-002')") {{scope.row.employeeStatus ? '启用' : '停用'}}
              .table-item-btn.info(@click="delItem(scope.row)" v-if="$permission('AB-06001-003')") 删除
  .table-pagination
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    )
  add-member-dialog(:show="showDialog", :item="currentItem" @close="closeDialog" @confirm="confirmDialog")
</template>

<script>
import { getMemberList, getRoleList, updateEmployeeStatus, postDeleteUser } from 'api'
import addMemberDialog from '@/components/modal/addMemberDialog'
export default {
  name: 'memberNanage',
  data () {
    return {
      list: [],
      page: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      searchString: '',
      roleId: '',
      roleList: [],
      showDialog: false,
      currentItem: null
    }
  },
  components: {
    addMemberDialog
  },
  methods: {
    changeSearch () {
      this.page = 1
      this.getList()
    },
    changeStatus () {
      this.page = 1
      this.getList()
    },
    currentChange (page) {
      this.page = page
      this.getList()
      // this.$router.replace({ name: this.$route.name, query: { ...this.$route.query, page: this.page } })
    },
    addMember () {
      this.showDialog = true
    },
    closeDialog () {
      this.showDialog = false
    },
    confirmDialog () {
      this.getList()
      this.showDialog = false
    },
    seeDetail (row) {
      this.showDialog = true
      this.currentItem = row
    },
    setStatus (row) {
      let title = !row.employeeStatus ? '是停用该成员？停用后该成员无法登录' : '是启用该成员'
      let info = !row.employeeStatus ? '停用提示' : '启用提示'
      this.$confirm(title, info, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: !row.employeeStatus ? 'warning' : 'success'
      }).then(async () => {
        try {
          await this.$axios.post(updateEmployeeStatus, { userName: row.userName, employeeStatus: row.employeeStatus ? 0 : 1 })
          this.$message.success('设置成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    delItem (row) {
      this.$confirm('是否删除该成员？删除后该成员数据无法恢复', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        try {
          await this.$axios.post(postDeleteUser, { userId: row.employeeId })
          this.$message.success('删除成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    async getList () {
      try {
        this.loading = true
        let query = { ...this.$route.query }
        query.currentPage = this.page 
        query.pageLines = this.pageSize
        if (this.searchString) query.data = this.searchString
        if (this.roleId) query.roleId = this.roleId
        const { ResultSet } = await this.$axios.post(getMemberList, { ...query })
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.onloadMemberList
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getRoleList () {
      try {
        const { ResultSet } = await this.$axios.post(getRoleList)
        this.roleList = ResultSet.userData.userRoleList
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(async vm => {
      vm.getRoleList()
      vm.getList()
    })
  },
  beforeRouteUpdate (to, from, next) {
    next()
    this.getList()
  }
} 
</script>

<style lang="scss" scoped>
.designer {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  .el-input {
    width: 202px;
    margin-right: 16px;
  }
  .header-search {
    display: flex;
    align-items: center;
  }
  .header-info {
    font-size: 14px;
    line-height: 22px;
    color: #8B8EAB;
  }
}
.designer-table {
  height: calc(100% - 150px);
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
.table-item-bd {
  &.flex {
    display: flex;
    align-items: center;
  }
  &.click {
    cursor: pointer;
  }
  .table-item-head {
    width: 24px;
    height: 24px;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    border-radius: 4px;
    margin-right: 8px;
  }
  .table-item-name {
    font-size: 14px;
    line-height: 22px;
    color: #FF2E42;
  }
}
.table-item-status {
  display: flex;
  align-items: center;
  .iconfont {
    font-size: 16px;
    margin-right: 8px;
  }
  .icon-normal {
    color: #32C37D;
  }
  .icon-block-up {
    color: #FBB142;
  }
}
.table-item-handle {
  display: flex;
  .table-item-btn {
    margin-right: 8px;
    padding: 5px 8px;
    font-size: 12px;
    line-height: 20px;
    border-radius: 6px;
    cursor: pointer;
    &.warning {
      background: #FDEFF1;
      color: #FF2E42;
    }
    &.normal {
      background: #F3F0FF;
      color: #7362F0;
    }
    &.info {
      background: #F6F6F9;
      color: #3E4373;
    }
  }
}
</style>
